package com.fzshang.shopping.wx.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzshang.shopping.commen.bean.Classification;
import com.fzshang.shopping.commen.bean.layuiBean.ResultPage;
import com.fzshang.shopping.commen.bean.layuiBean.VoPage;
import com.fzshang.shopping.commen.controller.BaseController;
import com.fzshang.shopping.commen.entity.ApiResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2020-03-07
 */
@Controller
@RequestMapping("wx/classification")
public class WXClassificationController extends BaseController {

    /**
     * @param id
     * @return 根据分类id查询
     */
     @RequestMapping(value = "selectByClassificationId")
    @ResponseBody
    public ApiResult selectByClassificationId(Long id){
         QueryWrapper queryWrapper = new QueryWrapper();
         queryWrapper.eq("classification_id",id);
         List<Classification> classifications = classificationService.list(queryWrapper);
         return ApiResult.ok(selectByType(classifications));
     }


    public static List<Classification> selectByType(List<Classification> orderList) {
        Set<Classification> set = new TreeSet<Classification>(new Comparator<Classification>() {
            @Override
            public int compare(Classification a, Classification b) {
                // 字符串则按照asicc码升序排列
                return a.getType().compareTo(b.getType());
            }
        });
        set.addAll(orderList);
        return new ArrayList<Classification>(set);
    }

    /**
     * @param id
     * @return 根据类型查询
     */
    @RequestMapping(value = "selectName")
    @ResponseBody
    public ApiResult selectName(String name,Long id){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("type",name);
        queryWrapper.eq("classification_id",id);
        List<Classification> classifications = classificationService.list(queryWrapper);
//        classificationService.selectByCreateTimeAsc(name, id)
        return ApiResult.ok(classifications);
    }

    /**
     * @param id
     * @return 根据id查询图片
     */
    @RequestMapping(value = "selectImage")
    @ResponseBody
    public ApiResult selectImage(Long id){
        return ApiResult.ok(commodityClassificationService.getById(id));
    }


    /**
     * @param name
     * @return 微信搜索类型
     */
    @RequestMapping(value = "selectByName")
    @ResponseBody
    public ApiResult selectByName(String name){
        return ApiResult.ok(classificationService.selectByName(name));
    }

    /**
     * @param id
     * @return 根据类型查询
     */
    @RequestMapping(value = "selectNameAndType")
    @ResponseBody
    public ApiResult selectNameAndType(String name,String type){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("type",type);
        queryWrapper.eq("name",name);
        List<Classification> classifications = classificationService.list(queryWrapper);
//        classificationService.selectByCreateTimeAsc(name, id)
        return ApiResult.ok(classifications);
    }
}
